package com.example.apiproject.service.impl;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.example.apiproject.entity.Activities;
import com.example.apiproject.entity.JoinActivity;
import com.example.apiproject.dao.JoinActivityDao;
import com.example.apiproject.jwt.jwtutil;
import com.example.apiproject.service.ActivitiesService;
import com.example.apiproject.service.JoinActivityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;

/**
 * (JoinActivity)表服务实现类
 *
 * @author makejava
 * @since 2023-09-23 13:40:57
 */
@Service("joinActivityService")
public class JoinActivityServiceImpl implements JoinActivityService {
    @Resource
    private JoinActivityDao joinActivityDao;
    @Autowired
    private jwtutil jwtUtil;
    @Autowired
    private ActivitiesService activitiesService;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public JoinActivity queryById(Integer id) {
        return this.joinActivityDao.queryById(id);
    }

    /**
     * 分页查询
     *
     * @param joinActivity 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */
    @Override
    public Page<JoinActivity> queryByPage(JoinActivity joinActivity, PageRequest pageRequest) {
        long total = this.joinActivityDao.count(joinActivity);
        return new PageImpl<>(this.joinActivityDao.queryAllByLimit(joinActivity, pageRequest), pageRequest, total);
    }

    /**
     * 新增数据
     *
     * @param joinActivity 实例对象
     * @return 实例对象
     */
    @Override
    public JoinActivity insert(JoinActivity joinActivity) {
        this.joinActivityDao.insert(joinActivity);
        return joinActivity;
    }

    /**
     * 修改数据
     *
     * @param joinActivity 实例对象
     * @return 实例对象
     */
    @Override
    public JoinActivity update(JoinActivity joinActivity) {
        this.joinActivityDao.update(joinActivity);
        return this.queryById(joinActivity.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.joinActivityDao.deleteById(id) > 0;
    }

    /**
     * 报名活动
     * @param request
     * @param id 活动id
     * @return
     */
    @Override

    public int signUp(int userId, String id,String description) {
      //是否已经报过名

      //判断当前报名人数是否小于活动规模
        Activities activity = activitiesService.queryById(id);
        if (activity.getAttendNum() < maxValue(activity.getValue())){

            JoinActivity joinActivity = new JoinActivity();
            joinActivity.setActivityId(id);
            joinActivity.setUserId(userId);

            joinActivity.setCreateTime(LocalDateTime.now());
            joinActivity.setStatus(1);
            joinActivity.setDescription(description);
            activity.setAttendNum(activity.getAttendNum() + 1);
            activitiesService.update(activity);
            return joinActivityDao.insert(joinActivity);
        }
        else return 0;

    }

    /**
     * 查询是否已经报名
     * @param userId
     * @param id
     * @return
     */
    @Override
    public int querySigned(int userId, String id) {

        return joinActivityDao.querySigned(userId,id);
    }

    /**
     * 得出活动规模区间最大值
     * @param value
     * @return
     */
    public int maxValue(int value){
        if (value == 0) return 10;
        if (value == 1) return 20;
        if (value == 2) return 50;
        if (value == 3) return 100;
        else return Integer.MAX_VALUE;
    }
}
